import cv2
from utils.common import load_image, show_image, make_dirs  # 导入公共函数
from utils.config import exp2_gray_path, exp2_equalized_path, exp2_stretched_path, exp2_output_dir  # 导入输出路径

# 确保输出目录存在
make_dirs(exp2_output_dir)

# 加载灰度图像
gray_image = cv2.imread(exp2_gray_path, cv2.IMREAD_GRAYSCALE)

# 直方图均衡化
equalized_image = cv2.equalizeHist(gray_image)
show_image("Equalized Image", equalized_image)

# 对比度拉伸
min_val, max_val, _, _ = cv2.minMaxLoc(gray_image)
stretched_image = cv2.convertScaleAbs(gray_image, alpha=255.0 / (max_val - min_val), beta=-min_val * 255.0 / (max_val - min_val))
show_image("Stretched Image", stretched_image)

# 保存结果
cv2.imwrite(exp2_equalized_path, equalized_image)
cv2.imwrite(exp2_stretched_path, stretched_image)
print(f"直方图均衡化结果已保存到：{exp2_equalized_path}")
print(f"对比度拉伸结果已保存到：{exp2_stretched_path}")